package com.example.tests;


import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

import com.example.pages.CreateUserPage;
import com.example.pages.EditUserPage;
import com.example.pages.InternalPage;
import com.example.pages.LoginPage;



public class TestPageObjects{
	private WebDriver driver;
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();
	private LoginPage LoginPage;
	private EditUserPage EditUserPage;
	private CreateUserPage CreateUserPage;
	private InternalPage InternalPage;
	
	
	@Before
	public void setUp() throws Exception {
		driver = new FirefoxDriver();
		baseUrl = "http://localhost/";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	@Test
	public void testChangeUser() throws Exception {
		goToMainPage();
		login("admin", "1");
		assertFalse("Incorrect user name or Password!",isFalseUserNameOrPassword());
		goToMyProfile();
		assertTrue("It doesn't edit user page!",isFormMyProfile());
		changePassword("1", "1");
		//assertTrue("It doesn't create user page!", formCreateUser());
		createUser("test","test@test.com",  "1", "1");
		logout();
		assertTrue("It doesn't login form!", isOnLoginPage());
		login("test", "1");
		assertFalse("Incorrect user name or Password!",isFalseUserNameOrPassword());
		logout();
	
	}
	
	private boolean isOnLoginPage() {
		return driver.findElements(By.id("loginform")).size()>0;
	}
	
	private boolean isFormCreateUser() {
		return driver.findElement(By.className("content")).findElement(By.tagName("h3")).getText().equals("New user");
	}

	private boolean isFormMyProfile() {
		return driver.findElement(By.className("content")).findElement(By.tagName("h2")).getText().equals("Edit admin");
	}

	private boolean isFalseUserNameOrPassword() {
		return driver.findElements(By.className("error")).size()>0;
	}

	private void logout() {
	InternalPage.menuLogoutLink.click();
	driver.switchTo().alert().accept();
	}

	private void createUser(String newUsername, String email, String password, String password2) {
		CreateUserPage.userNameField.clear();
		CreateUserPage.userNameField.sendKeys(newUsername);
		CreateUserPage.emailField.clear();
		CreateUserPage.emailField.sendKeys(newUsername);
		changePassword(password, password2);
	}

	private void changePassword(String password, String password2) {
		EditUserPage.passwordField.clear();	
		EditUserPage.passwordField.sendKeys(password);
		EditUserPage.password2Field.clear();	
		EditUserPage.password2Field.sendKeys(password);
		submitUserCreation();
	}

	private void submitUserCreation() {
		EditUserPage.saveButton.click();
	}

	private void goToMyProfile() {
		driver.findElement(By.linkText("My profile")).click();
		EditUserPage = PageFactory.initElements(driver, EditUserPage.class);
	}

	private void login(String username, String password) {
		LoginPage.userNameField.clear();
		LoginPage.userNameField.sendKeys(username);
		LoginPage.passwordField.clear();
		LoginPage.passwordField.sendKeys(password);
		LoginPage.loginButton.click();
	}

	private void goToMainPage() {
		driver.get(baseUrl + "/php4dvd/");
		LoginPage = PageFactory.initElements(driver, LoginPage.class);
	}

	@After
	public void tearDown() throws Exception {
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
}
